/*
 * Copyright (C) 2009 TeamCity Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cdmdotnet.android.teamcity.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cdmdotnet.android.teamcity.R;

import android.content.Intent;
import android.net.Uri;

public class TCBuildTypeProxy extends TCEntityLoader {
	
	public static final String NAME = "buildtype.name";
	public static final String ID = "buildtype.id";
	public static final String HREF = "buildtype.href";

	private List<TCBuildProxy> mBuilds = new ArrayList<TCBuildProxy> ();

	public TCBuildTypeProxy(Intent intent) {
		super(intent);
	}
	
	public TCBuildTypeProxy (Intent sender, JSONObject jsObject) {
		this (sender);
		init (jsObject);
	}
	
	private void init (JSONObject jsObject) {
		Intent intent = getData();
		intent.putExtra(NAME, jsObject.optString("name"));
		intent.putExtra(ID, jsObject.optString("id"));
		intent.putExtra(HREF, jsObject.optString("href"));
		// updating Intent URI

		Uri uri = intent.getData();
		String port = ":" + uri.getPort();
		if (uri.getPort() == -1)
			port = "";
		uri = Uri.parse
		(
			uri.getScheme()
				.concat("://")
				.concat(uri.getAuthority())
				.concat(port)
				.concat(intent.getStringExtra(HREF))
				.concat("/builds")
		);

		intent.setData(uri);
	}

	public void loadFromJSON(String jsonString) throws JSONException
	{
		// A Simple JSONObject Creation
		JSONObject jsBuilds=new JSONObject(jsonString);

		// ---  Workaround: When there's just one element, JSON returns one object instead of array
		JSONObject jsBuild = jsBuilds.optJSONObject("build");
		if (null != jsBuild) {
			mBuilds.add (new TCBuildProxy(getData(), jsBuild));
		}
		// ---
		
		JSONArray jsBuildsArray=jsBuilds.optJSONArray("build");
		if (null != jsBuildsArray) {
			for(int i=0; i<jsBuildsArray.length(); i++)
			{
				mBuilds.add (new TCBuildProxy(getData(), jsBuildsArray.getJSONObject(i)));
			}
		}
	}
	
	public List<TCBuildProxy> getBuilds() {
		return mBuilds;
	}
	
	public int getLastBuildStatus() {
//TODO: Dirty implementation, need to sort them properly by the 'number'
		if (mBuilds.isEmpty()) {
			return TCBuildProxy.BUILD_NEVER_RAN;
		}
		return mBuilds.get(0).getData().getIntExtra(TCBuildProxy.BUILD_STATUS, TCBuildProxy.BUILD_UNKNOWN);
	}
	
	//TODO: Localize
	public String getLastBuildStatusString() {
		if (getLoadingState() == LOADING_STATE_LOADED) {
	   		switch (getLastBuildStatus()) {
				case TCBuildProxy.BUILD_SUCCESS:
					return "Successful";
				case TCBuildProxy.BUILD_FAILURE:
					return "Failed";
				case TCBuildProxy.BUILD_NEVER_RAN:
					return "Build never ran";
				case TCBuildProxy.BUILD_UNKNOWN:
					return "Unknown";
			}
		}
		return "";
	}
	
	public int getLastBuildStatusBackground() {
		if (getLoadingState() == LOADING_STATE_LOADED) {
	   		switch (getLastBuildStatus()) {
	   		case TCBuildProxy.BUILD_SUCCESS:
				return R.color.green0; //R.drawable.gradient_green;
	   		case TCBuildProxy.BUILD_FAILURE:
   				return R.color.red0; //R.drawable.gradient_red;
	   		case TCBuildProxy.BUILD_NEVER_RAN:
	   			return android.R.color.white;
	   		case TCBuildProxy.BUILD_UNKNOWN:	
	   			return android.R.color.white;
	   		}
		}
		return android.R.color.white;
	}
	
	// Status icon cache
	private static int[] sIconIds = new int [] 
	                    {R.drawable.success, R.drawable.error,R.drawable.paused, R.drawable.buildgray};

	public int getLastBuildStatusIcon() {
		if (getLoadingState() == LOADING_STATE_LOADED) {
			return sIconIds[getLastBuildStatus()];
		}
		return 0;
	}
	
	@Override
	public String toString() {
		return getData().getStringExtra(NAME);
	}

}